package zdy.web.orm.mybatis2;
import java.io.InputStream;  

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
public class mybatisFactory {  
	  
    private static SqlSessionFactory sqlSessionFactory;  
      
    public static SqlSessionFactory getFactory(){  
        if(sqlSessionFactory==null){  
            InputStream inputStream=null;  
            try{  
                inputStream=Resources.getResourceAsStream("mybatis2.xml");  
                sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream); 
                System.out.println(sqlSessionFactory);
            }catch(Exception e){  
                e.printStackTrace();  
            }  
        }  
        return sqlSessionFactory;  
    }  
    /** 
     * 获取SqlSession 
     * @return SqlSession 
     */
    public static SqlSession getSqlSession(){  
        return getFactory().openSession();  
    }  
   
    /** 
    * 获取SqlSession 
    * @param isAutoCommit 
    * true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 
    * false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务，这时就需要我们手动调用sqlSession.commit()提交事务 
    * @return SqlSession 
    */
    public static SqlSession getSqlSession(boolean isAutoCommit) { 
    return getFactory().openSession(isAutoCommit); 
    } 

}  